/*
 * Copyright 2018-present KunMinX
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.nsgf.library.ui.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.kunminx.binding_recyclerview.adapter.SimpleDataBindingAdapter;
import com.nsgf.library.R;
import com.nsgf.library.data.bean.WorkOrderDetailBean;
import com.nsgf.library.databinding.ItemManagerWorkOrderDamBinding;
import com.nsgf.skjz.utils.SPUtil;
import com.nsgf.skjz.utils.StringUtil;

/**
 * Create by GC at 20/4/19
 */
public class ManagerDamListAdapter extends SimpleDataBindingPositionAdapter<WorkOrderDetailBean.DataBean.PositionVOSBean, ItemManagerWorkOrderDamBinding> {

    int status;

    public ManagerDamListAdapter(Context context) {
        super(context, R.layout.item_manager_work_order_dam, DiffUtils.getInstance().getmWorkOrderDetailItemCallback());

    }

    public ManagerDamListAdapter(Context context, int status) {
        super(context, R.layout.item_manager_work_order_dam, DiffUtils.getInstance().getmWorkOrderDetailItemCallback());
        this.status = status;
    }

    @Override
    protected void onBindItem(ItemManagerWorkOrderDamBinding binding,
                              WorkOrderDetailBean.DataBean.PositionVOSBean item,
                              ViewHolder holder, int position) {
        binding.setAlbum(item);

        Drawable drawableRight = null;

        if (position == 0) {
            binding.viewLineUp.setVisibility(View.INVISIBLE);
        } else if (position == getItemCount() - 1) {
            binding.viewLineDown.setVisibility(View.INVISIBLE);
        } else {
            binding.viewLineUp.setVisibility(View.VISIBLE);
            binding.viewLineDown.setVisibility(View.VISIBLE);
        }

        if (status == 0) {
            switch (item.getStatus()) {
                case 1:
                    binding.itemNsgfViewOval.setBackgroundResource(R.drawable.ic_manager_work_order_list_oval);
                    binding.tvItemManagerWorkOrderDamItemName.setBackgroundResource(R.drawable.bg_manager_work_order_dam_item);
                    drawableRight = mContext.getResources().getDrawable(R.drawable.ic_manager_wordk_order_dam_success);
                    break;
                case 2:
                    binding.itemNsgfViewOval.setBackgroundResource(R.drawable.ic_manager_work_order_list_oval_red);
                    binding.tvItemManagerWorkOrderDamItemName.setBackgroundResource(R.drawable.bg_manager_work_order_dam_item_red);
                    drawableRight = mContext.getResources().getDrawable(R.drawable.ic_manager_wordk_order_dam_fail);
                    break;
                case 3:
                    binding.itemNsgfViewOval.setBackgroundResource(R.drawable.ic_manager_work_order_list_oval_bule);
                    binding.tvItemManagerWorkOrderDamItemName.setBackgroundResource(R.drawable.bg_manager_work_order_dam_item_bule);
                    drawableRight = mContext.getResources().getDrawable(R.drawable.ic_manager_wordk_order_dam_conduct);
                    break;
                default:
                    binding.itemNsgfViewOval.setBackgroundResource(R.drawable.ic_manager_work_order_list_oval_green);
                    binding.tvItemManagerWorkOrderDamItemName.setBackgroundResource(R.drawable.bg_manager_work_order_dam_item_green);
                    break;
            }
        } else {
            binding.itemNsgfViewOval.setBackgroundResource(R.drawable.ic_manager_work_order_list_oval);
            binding.tvItemManagerWorkOrderDamItemName.setBackgroundResource(R.drawable.bg_manager_work_order_dam_item);
            drawableRight = mContext.getResources().getDrawable(R.drawable.ic_manager_wordk_order_dam_success);
        }
        if (null != drawableRight) {
            int h = drawableRight.getIntrinsicHeight();
            int w = drawableRight.getIntrinsicWidth();
            drawableRight.setBounds(0, 0, w, h);
            binding.tvItemManagerWorkOrderDamItemName.setCompoundDrawables(null, null, drawableRight, null);
        } else
            binding.tvItemManagerWorkOrderDamItemName.setCompoundDrawables(null, null, null, null);


        String src = SPUtil.get("workOrder", "", mContext);
        if (!StringUtil.isEmpty(src)) {
            WorkOrderDetailBean workOrderDetailBean = new Gson().fromJson(src, WorkOrderDetailBean.class);

            if (null != workOrderDetailBean.getData().getAbnormalList()) {
                for (WorkOrderDetailBean.DataBean.PtWorkOrderAbnormal vosBean : workOrderDetailBean.getData().getAbnormalList()) {

                    if (vosBean.getPositionCode().equals(item.getPositionCode())) {
                        item.setStatus(1);
                    }
                }
            }
        }
    }
}
